package com.bigtotoro.shuiyincamera;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.bigtotoro.shuiyincamera.R;
import com.bigtotoro.shuiyincamera.model.BaseResponse;
import com.bigtotoro.shuiyincamera.model.VersionDetails;
import com.bigtotoro.shuiyincamera.model.VersionResponse;
import com.bigtotoro.shuiyincamera.util.MyUtils;
import com.bigtotoro.shuiyincamera.util.SystemUtils;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.umeng.analytics.MobclickAgent;

public class SettingActivity extends BaseActivity implements View.OnClickListener {

    private TextView tv_version;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        initBase();

        initView();

    }

    private String currentVersion = "";
    private void initView() {
        tv_version = findViewById(R.id.tv_version);

        currentVersion = SystemUtils.getVersion();
        tv_version.setText("v" + currentVersion ); //获取当前版本号

        findViewById(R.id.btn_my_arts).setOnClickListener(this); //作品列表
        findViewById(R.id.btn_score_us).setOnClickListener(this); //评分
        findViewById(R.id.btn_feedback).setOnClickListener(this); //反馈
        findViewById(R.id.btn_xieyi).setOnClickListener(this); //用户协议
        findViewById(R.id.btn_about_us).setOnClickListener(this); //关于我们
        findViewById(R.id.btn_check_version).setOnClickListener(this); //检查更新
    }

    public static void goAppShop(Context context) {
        try {
            Uri uri = Uri.parse("market://details?id=" + context.getPackageName());
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        } catch (Exception e){
            Toast.makeText(context, R.string.warning01, Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
    }

    private void checkVersion() {
        MyUtils.checkVersion(SettingActivity.this, 1, new MyUtils.IntegerCallback() {
            @Override
            public void callback(int data) {
                if (data == 2){ //有新版本

                }else if (data == -2){
                }
            }
        });
    }



    // 两次点击按钮之间的点击间隔不能少于3000毫秒
    private static final int MIN_CLICK_DELAY_TIME = 3000;
    private static long lastClickTime;
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back:
                finish();
                break;
            case R.id.btn_my_arts://作品列表
                startActivity(new Intent(SettingActivity.this,MyArtListActivity.class));
                break;
            case R.id.btn_score_us://评分
                goAppShop(SettingActivity.this);
                break;
            case R.id.btn_feedback://反馈
                startActivity(new Intent(SettingActivity.this,FeedbackActivity.class));
                break;
            case R.id.btn_xieyi://用户协议
                startActivity(new Intent(SettingActivity.this,XieyiActivity.class));
                break;
            case R.id.btn_about_us://关于我们
                startActivity(new Intent(SettingActivity.this,AboutUsActivity.class));
                break;
            case R.id.btn_check_version://检查更新
                long curClickTime = System.currentTimeMillis();
                if((curClickTime - lastClickTime) >= MIN_CLICK_DELAY_TIME) {
                    // 超过点击间隔后再将lastClickTime重置为当前点击时间
                    lastClickTime = curClickTime;
                    checkVersion();
                }
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("SettingActivity");//统计页面
        MobclickAgent.onResume(this);
    }
    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("SettingActivity");//统计页面
        MobclickAgent.onPause(this);
    }
}
